package com.wwr.config;

import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author：wwr
 * @date：2018/7/10
 * @since：jdk:1.8
 */

@Component
public class BaseInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        System.out.println("自定义拦截器......");
        String url = request.getRequestURI();
        /*try{
            User user = (User)request.getSession().getAttribute("user");
            User adminUser = (User)request.getSession().getAttribute("currentUser");
            //如果访问为url地址包含/admin
            if(url.indexOf("/admin")!=-1){
                //检测session是否为空
                if(user==null) {
                    //跳转到登录页面 这里重定向的地址是controller地址
                    response.sendRedirect("/pub/login");
                    return false;
                }else{
                    //如果有放行
                    return true;
                }
            }
            if(url.indexOf("/manage")!=-1 && !(url.contains("/login"))){
                //检测session是否为空
                if(adminUser==null) {
                    //跳转到登录页面 这里重定向的地址是controller地址
                    response.sendRedirect("/manage/view/login");
                    return false;
                }else{
                    //如果有放行
                    return true;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }*/
        //所有的连接放行出/admin的除外
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }


}

